﻿using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using myMVCdb.Data;
using myMVCdb.Models;
using myMVCdb.ParameterModel;
using myMVCdb.ViewModel;
namespace myMVCdb.Controllers
{
    public class MessagesController : Controller
    {
        private myMVCdbContext db = new myMVCdbContext();

      
        public ActionResult Index()
        {
            
            var id = 1;
            var currentUser = db.Users.Where(x => x.Id == id).SingleOrDefault();

       
            var userList = db.Users.ToList();
    
            var msgList = db.Messages.ToList();
       
            var comList = db.Comments.ToList();

      
            var msgViewModelList = new List<MessageViewModel>();

           
            foreach (var msg in msgList)
            {
              
                var tmpComList = new List<CommentViewModel>();
        
                var currentMsgComments = comList.Where(x => x.MsgId == msg.Id).ToList();


              
                foreach (var c in currentMsgComments)
                {
                    tmpComList.Add(new CommentViewModel
                    {
                        FromUserName = userList.SingleOrDefault(x => x.Id == c.FromUserId).UserName,// 根据当前的评论用户id，从用户中查找对应用户，并且获得用户的名称
                        ToUserName = userList.SingleOrDefault(x => x.Id == msgList.SingleOrDefault(t => t.Id == c.MsgId).FromUserId).UserName,// 
                        Comment = c.Content
                    });
                }

              
                msgViewModelList.Add(new MessageViewModel
                {
                    Id = msg.Id,
                    FromUserId = msg.FromUserId,
                    Content = msg.Content,
                    Comments = tmpComList
                });
            }


            var data = new SayViewModel
            {
                Username = currentUser.UserName,
                Messages = msgViewModelList
            };


            return View(data);
        }

        
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // GET: Messages/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Messages/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,FromUserId,Content,CreateAt,UpdateAt,Version,Remarks")] Messages messages)
        {
            if (ModelState.IsValid)
            {
                db.Messages.Add(messages);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(messages);
        }

        // GET: Messages/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // POST: Messages/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,FromUserId,Content,CreateAt,UpdateAt,Version,Remarks")] Messages messages)
        {
            if (ModelState.IsValid)
            {
                db.Entry(messages).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(messages);
        }

        // GET: Messages/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // POST: Messages/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Messages messages = db.Messages.Find(id);
            db.Messages.Remove(messages);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult say(SayModel sayModel)
        {
            db.Messages.AddRange(new List<Messages>
            {
                new Messages
                {
                    Id=1,
                    Content="今天想我了吗"
                },
            });
            db.SaveChanges();
                return View();
        }
        public ActionResult comment()
        {
            db.Comments.AddRange(new List<Comments>
            {
                new Comments
                {
                    Content="今天天气好",
                    FromUserId=1,
                    
                }
            });
            db.SaveChanges();
            return View();
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

    }
}
